package com.amazon.spring8amazon.controller;


import com.amazon.spring8amazon.domain.Params;
import com.amazon.spring8amazon.domain.YibaiAmazonPublishSkuSelectResult;
import com.amazon.spring8amazon.mapper.YibaiAmazonPublishSkuSelectResultMapper;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.apache.catalina.connector.Request;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cep123
 * @date 2024/8/8 17:57
 */

@Controller
@RestController
@RequestMapping("/amazonCenter")
//@MapperScan("com.amazon.spring8amazon.mapper")
@DS("ck")
public class IndexController {
    @GetMapping("/index")
    public String index(Model model) {
//        为啥放model
        model.addAttribute("title", "传递的title");
        model.addAttribute("description","传递的description");
        model.addAttribute("keywords","传递的keywords");

        model.addAttribute("yu","yu");

        System.out.println(model);
        //返回模板
        return "/index";
    }





    @GetMapping("/search")
    public String search(Model model) {

        //返回模板
        return "/sos";
    }




    @Autowired
    private YibaiAmazonPublishSkuSelectResultMapper yibaiAmazonPublishSkuSelectResultMapper;
    @GetMapping("/tableTest")
    public String tableTest(Model model) {
        //采取分页的方式查询结果
        List<YibaiAmazonPublishSkuSelectResult> tables= yibaiAmazonPublishSkuSelectResultMapper.selectMany();

        //将查询结果放入model
        model.addAttribute("tables", tables);
        model.addAttribute("page", 1);
        //返回模板
        return "/table";
    }



    @GetMapping("/table")
    public String table(Model model, Integer page) {

//        List<YibaiAmazonPublishSkuSelectResult> tables= yibaiAmazonPublishSkuSelectResultMapper.selectMany();
        int sizeNum = 15; // 每页显示的条数
        int limit;
        // 计算limit 和 page
        if (page == null || page <= 1)  {
             limit = 0;
             page = 1;
        } else {
             limit = (page - 1) * sizeNum;
        }
        Params params = new Params();
        params.setLimit(limit);
        params.setOffset(sizeNum);
        model.addAttribute("nextPage", 2);

        //采取分页的方式查询结果
        List<YibaiAmazonPublishSkuSelectResult> tables= yibaiAmazonPublishSkuSelectResultMapper.selectManyByPage(params);

        model.addAttribute("page", page);
        //将查询结果放入model
        model.addAttribute("tables", tables);

        //返回模板
        return "/table";
    }



}
